<template>
  <div class="wrap">
    <pro-table
      ref="proTableInst"
      tableTitle="盘点列表"
      :columns="columns"
      :request="fetchInventoryPage"
    >
      <template #operation>
        <n-space>
          <n-button type="primary" @click="handleAddInventory">
            新增盘点
          </n-button>
        </n-space>
      </template>
    </pro-table>

    <pro-dialog
      v-model:showDialog="inventoryModalShow"
      :modalProps="modalProps"
      :showButton="true"
      confirmButtonText="确定"
      :showConfirmButton="handleType !== 'check'"
      :cancelButtonText="handleType === 'check' ? '关闭' : '取消'"
      :contentStyle="{ 'padding-left': '10px', 'padding-right': '10px' }"
      width="1000"
      height="500"
      :button-loading="confirmLoading"
      @handleConfirm="inventoryConfirm"
      @afterClosed="resetInventoryForm"
      v-bind="$attrs"
    >
      <pro-form
        ref="inventoryFormInst"
        v-model:form="inventoryForm"
        :formProps="{
          labelPlacement: 'left',
          labelWidth: '100px'
        }"
        :options="inventoryFormOptions"
      ></pro-form>
    </pro-dialog>
  </div>
</template>

<script lang="ts" setup name="Inventory">
  import { ProTableInst } from '@/components/pro-table/type'
  import { ProFormInst } from '@/components/pro-form/type'
  import {
    useInventoryApi,
    useInventoryProForm,
    useInventoryProTable
  } from './hooks'
  import { InventoryModel } from '@/api/model/inventory-model'
  import { useUser } from '@/store/modules/user'

  const proTableInst = ref<ProTableInst | null>(null)
  const inventoryFormInst = ref<ProFormInst | null>(null)

  const inventoryModalShow = ref(false)
  const handleType = ref<'add' | 'edit' | 'check'>('check')
  const handleTag = computed(() =>
    handleType.value === 'add'
      ? '新增盘点'
      : handleType.value === 'check'
      ? '盘点详情'
      : '修改盘点'
  )
  const modalProps = computed(() => {
    return {
      showIcon: false,
      title: handleTag.value
    }
  })

  const { userInfo } = storeToRefs(useUser())
  const { memberLoading, memberOptions } = useGetOrgMemberList()

  const { inventoryForm, resetInventoryForm, inventoryFormOptions } =
    useInventoryProForm({
      handleType,
      memberLoading,
      memberOptions
    })

  const handleAddInventory = () => {
    handleType.value = 'add'
    inventoryModalShow.value = true
  }

  const handleEditInventory = (row: InventoryModel, type: 'check' | 'edit') => {
    inventoryForm.value = row
    handleType.value = type
    inventoryModalShow.value = true
  }

  const inventoryConfirm = async () => {
    if (await inventoryFormInst.value?.handleSubmit()) {
      fetchAddOrEditInventory()
    }
  }

  const {
    fetchInventoryPage,
    fetchAddOrEditInventory,
    fetchDelInventory,
    confirmLoading
  } = useInventoryApi({
    handleType,
    handleTag,
    inventoryModalShow,
    inventoryForm,
    proTableInst,
    userInfo,
    memberOptions
  })
  const { columns } = useInventoryProTable({
    userInfo,
    handleEditInventory,
    fetchDelInventory
  })
</script>

<style lang="less" scoped></style>
